package org.dromara.mall.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.mall.domain.vo.TerCreateVo;
import org.dromara.mall.domain.vo.TerPayVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.mall.domain.vo.TerMallOrderVo;
import org.dromara.mall.domain.bo.TerMallOrderBo;
import org.dromara.mall.service.ITerMallOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单主信息
 * 前端访问路由地址为:/mall/mallOrder
 *
 * @author xch
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mallOrder")
public class TerMallOrderController extends BaseController {

    private final ITerMallOrderService terMallOrderService;
    /**
     * 查询订单主信息列表
     */
    @SaCheckPermission("mall:mallOrder:list")
    @GetMapping("/list")
    public TableDataInfo<TerMallOrderVo> list(TerMallOrderBo bo, PageQuery pageQuery) {
        return terMallOrderService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询订单主信息列表
     */
    @GetMapping("/wechat/list")
    public TableDataInfo<TerMallOrderVo> query(TerMallOrderBo bo, PageQuery pageQuery) {
        return terMallOrderService.queryOrderList(bo, pageQuery);
    }

    /**
     * 导出订单主信息列表
     */
    @SaCheckPermission("mall:mallOrder:export")
    @Log(title = "订单主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerMallOrderBo bo, HttpServletResponse response) {
        List<TerMallOrderVo> list = terMallOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单主信息", TerMallOrderVo.class, response);
    }
    @PostMapping("/wechat/refund")
    public R<Void> refund(@Validated(EditGroup.class) @RequestBody TerMallOrderBo bo) {
        return toAjax(terMallOrderService.refund(bo));
    }
    /**
     * 获取订单主信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:mallOrder:query")
    @GetMapping("/{id}")
    public R<TerMallOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(terMallOrderService.queryById(id));
    }

    /**
     * 新增订单主信息
     */
    @RepeatSubmit()
    @PostMapping("/wechat/create")
    public R<TerCreateVo> create(@Validated(AddGroup.class) @RequestBody TerMallOrderBo bo) {
        return R.ok(terMallOrderService.createOrder(bo));
    }
    @Log(title = "订单主信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/wechat/createCart")
    public R<TerCreateVo> createCart(@Validated(AddGroup.class) @RequestBody TerMallOrderBo bo){
        return R.ok(terMallOrderService.createCartOrder(bo));
    }
    @Log(title = "订单主信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/wechat/pay")
    public R<TerPayVo> pay(@Validated(AddGroup.class) @RequestBody TerMallOrderBo bo){
        return R.ok(terMallOrderService.payOrder(bo));
    }

    @RepeatSubmit()
    @PostMapping("/wechat/pickupCode")
    public R<TerPayVo> getPickupCode(@Validated(AddGroup.class) @RequestBody TerMallOrderBo bo){
        return R.ok(terMallOrderService.getPickupCode(bo));
    }
    /**
     * 修改订单主信息
     */
    @SaCheckPermission("mall:mallOrder:edit")
    @Log(title = "订单主信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerMallOrderBo bo) {
        return toAjax(terMallOrderService.updateByBo(bo));
    }

    /**
     * 删除订单主信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:mallOrder:remove")
    @Log(title = "订单主信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terMallOrderService.deleteWithValidByIds(List.of(ids), true));
    }



}
